import { _decorator, Component, instantiate, Node, Prefab } from 'cc';
import { BasePanel } from './BasePanel';
import { GoodItem } from './items/GoodItem';
import { BaseItem } from '../items/BaseItem';
import { _item } from '../Main';
import { Adsdk } from '../scenes/Adsdk';
const { ccclass, property } = _decorator;

@ccclass('ShopPanel')
export class ShopPanel extends BasePanel {
    
    panelName: string = '商店'

    @property(Prefab)
    goodItemPrefab: Prefab = null!

    @property(Node)
    itemsNode: Node = null!

    items: { item: BaseItem, amount: number }[] = []

    start(): void {
        this.init(this.getAllItems())
        super.start()
        Adsdk.instance.showInterstitialAd()
    }

    getAllItems() {
        return [{ item: _item.items.Coin, amount: 100 }, 
            { item: _item.items.MDClear, amount: 1 }, 
            { item: _item.items.MDRefresh, amount: 1 }, 
            { item: _item.items.MDUncover, amount: 1 }]
    }

    init(items: { item: BaseItem, amount: number }[]) {
        this.items = items
        for (let i = 0; i < this.items.length; i++) {
            let item: { item: BaseItem, amount: number } = this.items[i]
            let gn: Node = instantiate(this.goodItemPrefab)
            gn.parent = this.itemsNode
            let goodItem: GoodItem = gn.getComponent(GoodItem)
            goodItem.init(item.item, item.amount)
        }
    }
}


